/**
 * Created by IntelliJ IDEA.
 * User: christopher_moore
 * Date: Jul 25, 2007
 * Time: 7:11:04 AM
 * To change this template use File | Settings | File Templates.
 */

import java.util.*;

/**
 * A Snapshot is a collection of auctions found during an auction scan.
 * It is associated with a particular server-faction.
 */

public class Snapshot {
    private String serverFactionName;
    private HashMap<Integer, Auction> auctionList;
    private HashMap<String, String> auctionsByItemKey;

    public Snapshot(String serverFaction) {
        this.serverFactionName = serverFaction;
        this.auctionList = new HashMap<Integer, Auction>();
        this.auctionsByItemKey = new HashMap<String, String>();
    }

    public void addAuction(String line) {
        if (line.trim().startsWith("nil,")) {
            return;
        }
        // System.out.println("Adding auction string '" + line + "' to snapshot for '" + serverFactionName + "'");
        Auction newAuction = new Auction(line.trim());
        Integer auctionId = newAuction.getAuctionId();
        auctionList.put(auctionId, newAuction);
    }

    public void addAuctionIdByItemKey(String line) {
        // System.out.println("Adding auctionIdByItemKey string '" + line + "' to snapshot for '" + serverFactionName + "'");

        int delim1;
        int delim2;

        delim1 = line.indexOf("[\"");
        delim2 = line.indexOf("\"]");

        String itemKeyString = line.substring(delim1+2, delim2);
        // ItemKey newItemKey = new ItemKey(itemKeyString);

        delim1 = line.indexOf(" = ");
        StringBuffer auctionListString = new StringBuffer(line.substring(delim1+3));

        // If there is a single auction in the auctionListString it will simply be
        // xxxxx,
        // If there is more than one it will look like:  "xxxx;yyyy;zzzz",

        if (auctionListString.charAt(0) == '"') {
            // Trim the leading quote and the trailing ",
            auctionListString.deleteCharAt(0);
            auctionListString.deleteCharAt(auctionListString.length() - 1);
        }

        // Just trim the trailing ,
        auctionListString.deleteCharAt(auctionListString.length() - 1);

        auctionsByItemKey.put(itemKeyString, auctionListString.toString());
    }

    private ArrayList<Integer> getAuctionsByItemKey(String itemKey) {
        String listString;
        ArrayList result = new ArrayList();

        listString = auctionsByItemKey.get(itemKey);

        if (listString == null) {
            return null;
        }

        // The listString should be a ; separated list of integers.  We want to
        // create an ArrayList of Integer objects and return that

        StringTokenizer st = new StringTokenizer(listString, ";");
        while (st.hasMoreTokens()) {
            Integer auctionId = new Integer(st.nextToken());
            result.add(auctionId);
        }

        return result;
    }


    public Integer getMedianBuyout(String itemKey) {
        ArrayList<Integer> priceList;

        priceList = getPrices(itemKey);

        if (priceList == null) {
            return 0;
        }

        if (priceList.size() == 0) {
            return 0;
        }
        
        Collections.sort(priceList);

        int which = priceList.size() / 2;
        Integer median = priceList.get(which);

        return median;


    }

    public Integer getCount(String itemKey) {
        ArrayList<Integer> priceList;

        priceList = getPrices(itemKey);

        if (priceList == null) {
            return 0;
        }

        return priceList.size();

    }

    private ArrayList<Integer> getPrices(String itemKey) {
        ArrayList<Integer> auctions = getAuctionsByItemKey(itemKey);
        ArrayList<Integer> prices = new ArrayList<Integer>();

        if (auctions == null) {
            return null;
        }

        for (Integer auctionId : auctions) {
            Auction auction = auctionList.get(auctionId);
            prices.add(auction.getBuyoutPrice() / auction.getCount());
        }

        return prices;
    }
}
